<template>
	<div class="layout-padding-auto layout-padding-view">
		<!-- 搜索 -->
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
			<el-radio-group v-model="queryParams.state" @change="changeState">
				<el-radio :label="1">一级推广</el-radio>
				<el-radio :label="2">二级推广</el-radio>
			</el-radio-group>
		</el-form>

		<!-- 工具栏 -->
		<div class="hx-table-toolbar">
			<div></div>
			<right-toolbar :searchBtn="true" :refreshBtn="true" @search="showSearch = !showSearch" @refresh="initPage"></right-toolbar>
		</div>
		<!-- 列表 -->
		<el-table v-loading="loading" :data="tableData">
			<el-table-column prop="avatarUrl" label="头像">
				<template #default="scope">
					<el-image
						style="width: 50px; height: 50px"
						:src="scope.row.avatarUrl"
						:preview-src-list="[scope.row.avatarUrl]"
						fit="cover"
						:preview-teleported="true"
					/>
				</template>
			</el-table-column>

			<el-table-column prop="nickName" label="昵称"></el-table-column>
			<el-table-column prop="phone" label="手机号" />
			<el-table-column prop="sex" label="性别">
				<template #default="scope">{{ scope.row.sex == '1' ? '男' : scope.row.sex == '2' ? '女' : '未知' }}</template>
			</el-table-column>
			<el-table-column prop="userGrade" label="用户等级">
				<template #default="scope">{{ scope.row.userGrade == '0' ? '普通用户' : scope.row.userGrade == '1' ? '会员用户' : '' }}</template>
			</el-table-column>
			<el-table-column prop="userSource" label="来源">
				<template #default="scope">{{
					scope.row.userSource == 'WX-MA' ? '微信小程序' : scope.row.userSource == 'APP' ? 'APP' : scope.row.userSource == 'H5' ? '普通H5' : '未知'
				}}</template>
			</el-table-column>
			<el-table-column prop="province" label="所在省份"></el-table-column>
			<el-table-column prop="city" label="所在城市"></el-table-column>
			<el-table-column prop="createTime" label="创建时间" />
		</el-table>
		<!-- 分页 -->
		<pagination
			v-model:total="page.total"
			v-model:currentPage="page.currentPage"
			v-model:pageSize="page.pageSize"
			@changePage="initPage"
		></pagination>
	</div>
</template>
<script lang="ts">
import { defineComponent, ref, reactive, toRefs } from 'vue';
import { getTeamPage } from '/@/api/mall/userinfo';
export default defineComponent({
	props: {
		userId: {
			type: String,
			default: () => '',
		},
	},
	setup(props) {
		const data = reactive({
			queryParams: { state: 1 },
			page: {
				total: 0, // 总页数
				currentPage: 1, // 当前页数
				pageSize: 10, // 每页显示多少条
				ascs: [],
				descs: 'create_time',
			},
			tableData: [],
		});
		const showSearch = ref(true);
		const loading = ref(false);

		const initPage = async () => {
			loading.value = true;
			let params = {
				current: data.page.currentPage,
				size: data.page.pageSize,
				ascs: data.page.ascs,
				descs: data.page.descs,
				parentId: data.queryParams.state == 1 ? props.userId : '',
				secondParentId: data.queryParams.state == 2 ? props.userId : '',
			};
			await getTeamPage(params)
				.then((response) => {
					data.tableData = response.data.data.records;
					data.page.total = response.data.data.total;
					loading.value = false;
				})
				.catch(() => {
					loading.value = false;
				});
		};
		/**
		 * 重置搜索表单
		 */
		const resetQuery = () => {
			data.queryParams = { state: 1 };
		};
		const changeState = (value: number) => {
			initPage();
		};
		initPage();
		return {
			loading,
			showSearch,
			initPage,
			resetQuery,
			changeState,
			...toRefs(data),
		};
	},
});
</script>
<style scoped></style>
